package model;

import java.util.ArrayList;

public class MonteCarloThreadSimulation extends Thread {
	private MonteCarloSimulation mcs;
	public MonteCarloThreadSimulation(MonteCarloSimulation mcs) {
		super();
		this.mcs = mcs;
	}
	
	synchronized private void sSteps() throws InterruptedException {
		ArrayList <MonteCarloThreadSteps> alTSteps = new ArrayList<MonteCarloThreadSteps>(mcs.getnSteps());
		ArrayList <Thread> alT = new ArrayList <Thread>(mcs.getnSteps());
		mcs.setStockPrice(mcs.getStockPrice());
		
		for(int i = 0; i < mcs.getnSteps(); i++) {
			alTSteps.add(new MonteCarloThreadSteps(mcs));
			alT.add(new Thread(alTSteps.get(i)));
			alT.get(i).start();
		}
		
		for(int i = 0; i < mcs.getnSteps(); i++) 
			alT.get(i).join();
		mcs.setSum(mcs.getSum() + Math.max((mcs.getZ()*(mcs.getStockPrice() - mcs.getStrikePrice())),0));
	}
	
	@Override
	public void run() {
		try {
			sSteps();
		} 
		catch (InterruptedException e) {
			e.printStackTrace();
		}		
	}
}